﻿/******************************************************************************
 *  Author：       Commscope ITDC Suzhou Web Team 
 *  CreateDate：   21/02/2012 2:29:28 PM
 *
 *
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using EasyDLL.Web.UI;

namespace EasyDLL.Web.AutoValidate
{
    public class FormManager : Control
    {
        public void FormToModel(object model)
        {
            if (model == null)
            {
                return;
            }

            Type type = model.GetType();

            List<Control> controlList = new List<Control>();
            GetControls(Page.Form, ref controlList);

            PropertyInfo[] propertyInfos = type.GetProperties();
            foreach (PropertyInfo property in propertyInfos)
            {
                //遍历模型类，寻找标记了表单的属性
                object[] customAttributes = property.GetCustomAttributes(typeof(FormBindAttribute), true);

                if (customAttributes.Length > 0)
                {
                    FormBindAttribute formBindAttribute = customAttributes[0] as FormBindAttribute;
                    if (formBindAttribute != null)
                    {
                        foreach (Control control in controlList)
                        {
                            if (control.ID == formBindAttribute.controlId)
                            {
                                //如果属性指定的controlId存在于页面控件树中，则绑定
                                property.SetValue(model, ControlWrapper.GetControlValue(control), null);
                            }
                        }
                    }
                }
            }
        }

        public void ModelToForm(object model)
        {
            if (model == null)
            {
                return;
            }

            Type type = model.GetType();

            List<Control> controlList = new List<Control>();
            GetControls(Page.Form, ref controlList);

            PropertyInfo[] propertyInfos = type.GetProperties();
            foreach (PropertyInfo modelProperty in propertyInfos)
            {
                //遍历模型类，寻找标记了表单的属性
                object[] customAttributes = modelProperty.GetCustomAttributes(typeof(FormBindAttribute), true);

                if (customAttributes.Length > 0)
                {
                    FormBindAttribute formBindAttribute = customAttributes[0] as FormBindAttribute;
                    if (formBindAttribute != null)
                    {
                        foreach (Control control in controlList)
                        {
                            if (control.ID == formBindAttribute.controlId)
                            {
                                //如果属性指定的controlId存在于页面控件树中，则绑定
                                object modelValue = modelProperty.GetValue(model, null);
                                ControlWrapper.SetControlValue(control, modelValue);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 递归得到某个控件下所有的子控件(不包括自己)
        /// </summary>
        /// <param name="control"></param>
        /// <param name="controlList"></param>
        public void GetControls(Control control, ref List<Control> controlList)
        {
            if (control.Controls.Count == 0)
            {
                return;
            }

            foreach (Control subControl in control.Controls)
            {
                controlList.Add(subControl);
                GetControls(subControl, ref controlList);
            }

            return;
        }
    }
}
